# !/usr/bin/env python
# -*-coding:utf-8 -*-
# python -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade pip

"""
定制界面.

模块功能:
    1.
"""
from PySide2.QtCore import QSize, QPropertyAnimation
from PySide2.QtWidgets import QPushButton

from ui.custom.custom_window import *


class CustomGUI(CustomWindow):

    def __init__(self):
        super().__init__()

        self.animation = None  # 动画
        self.load_connect()  # 绑定信号与槽
        self.__set_icons()  # 设置按钮图标
        self.__load_settings()  # 载入设置信息

    # 绑定信号与槽
    def load_connect(self):

        self.ui.BtnSwitchTheme.clicked.connect(self.__switch_theme)  # 切换主题
        self.ui.BtnSwitchMenu.clicked.connect(self.__switch_menu)  # 展开收起菜单

    # 加载设置信息
    def __load_settings(self):

        self.setMinimumSize(GUISet.app_min_width, GUISet.app_min_height)
        self.resize(GUISet.app_min_width, GUISet.app_min_height)
        self.ui.AppDes.setText(GUISet.AppDes)

    # 设置按钮图标
    def __set_icons(self):
        # 标题栏按钮图标
        self.ui.BtnSwitchTheme.setIcon(QIcon(GUISet.btn_switch_theme))
        self.ui.BtnMin.setIcon(QIcon(GUISet.btn_min))
        self.ui.BtnMaxRestore.setIcon(QIcon(GUISet.btn_max))
        self.ui.BtnClose.setIcon(QIcon(GUISet.btn_close))

        self.ui.BtnSettings.setIcon(QIcon(GUISet.btn_settings))
        self.ui.BtnHome.setIcon(QIcon(GUISet.btn_home))
        self.ui.BtnSwitchMenu.setIcon(QIcon(GUISet.btn_switch_open))

        # 菜单栏按钮 图标大小
        for btn in self.ui.MenuBottomFR.findChildren(QPushButton):
            btn.setIconSize(QSize(18, 18))

    # 切换主题
    def __switch_theme(self):

        # 更改配置文件主题值
        if GUISet.theme == 'dark':
            GUISet.config.set_save('App', 'theme', 'light')
        else:
            GUISet.config.set_save('App', 'theme', 'dark')

        # 更换qss样式
        self.ui.AppWindow.setStyleSheet(GUISet.get_qss())
        # 重新加载图标
        self.__set_icons()

    # 展开|收起菜单
    def __switch_menu(self):
        if self.ui.MenuFR.width() == GUISet.menu_max_width:
            menu_width = GUISet.menu_min_width
            self.ui.BtnSwitchMenu.setIcon(QIcon(GUISet.btn_switch_open))
        else:
            menu_width = GUISet.menu_max_width
            self.ui.BtnSwitchMenu.setIcon(QIcon(GUISet.btn_switch_close))
        self.animations(self.ui.MenuFR, "minimumWidth", self.ui.MenuFR.width(), menu_width)

    # 动画
    def animations(self, widget, _property: str, start_value, end_value):
        self.animation = QPropertyAnimation(widget, _property.encode('utf-8'))
        self.animation.setDuration(GUISet.animation_time)
        self.animation.setStartValue(start_value)
        self.animation.setEndValue(end_value)
        self.animation.start()
